package com.goktech.olala.client.controller.backstage;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.goktech.olala.client.controller.basic.BasicController;
import com.goktech.olala.client.controller.basic.ResultStatus;
import com.goktech.olala.core.req.ReqGoodsBrand;
import com.goktech.olala.core.resp.RespGoodsBrandVo;
import com.goktech.olala.core.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author  wusq
 * 品牌管理
 */
@Controller
@RequestMapping(value = "/brandApi")
public class BrandController extends BasicController {

    @Autowired
    IGoodsService goodsService;

    @RequestMapping(value = "/queryBrandList.do")
    @ResponseBody
    public ModelMap queryBrandList(HttpServletRequest request, Integer sEcho,
                                   Integer iDisplayStart, Integer iDisplayLength) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = null;
        String respMsg = null;

        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            ReqGoodsBrand brandReq = build(params);

            int pageIndex = iDisplayStart/iDisplayLength + 1;
            PageInfo<RespGoodsBrandVo> pageInfo = goodsService.queryGoodsBrandByParam(brandReq, pageIndex, iDisplayLength);
            if (pageInfo != null && CollectionUtils.isEmpty(pageInfo.getList())) {
                respCode = ResultStatus.QUERY_NULL.getCode();
                respMsg = ResultStatus.QUERY_NULL.getMsg();
            }
            respData = super.respParamsFormat(respCode, respMsg, sEcho, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = ResultStatus.BAD_REQUEST.getCode();
            respMsg = ResultStatus.BAD_REQUEST.getMsg();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;

    }

    /**
     * 根据主键ID查询品牌信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryBrandInfoById.do")
    @ResponseBody
    public ModelMap queryBrandInfoById(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();
        Integer respCode = 10000;
        String respMsg = "查询成功!";

        JSONObject respData = null;
        try {
            JSONObject params = super.reqParamsFormat(request);
            Long brandId = params.getLong("brandId");

            RespGoodsBrandVo respBrandVo = goodsService.queryBrandInfoById(brandId);
            respData = super.respParamsFormat(respCode, respMsg, respBrandVo);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = ResultStatus.BAD_REQUEST.getCode();
            respMsg = ResultStatus.BAD_REQUEST.getMsg();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 品牌删除
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/removeBrandById.do", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap removeBrandById(HttpServletRequest request) {
        ModelMap resultMap = new ModelMap();
        JSONObject respData = new JSONObject();
        try {
            JSONObject params = super.reqParamsFormat(request);
            Long brandId = params.getLong("brandId");

            int result = goodsService.removeBrandById(brandId);
            respData = super.respParamsFormat(10000,"删除成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            respData = super.respParamsFormat(-10000,"服务异常", null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 保存品牌信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/saveBrandInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap saveBrandInfo(HttpServletRequest request) {

        ModelMap resultMap = new ModelMap();
        Integer respCode = ResultStatus.SUCCESS.getCode();
        String respMsg = ResultStatus.SUCCESS.getMsg();

        JSONObject respData = null;
        try {
            JSONObject params = super.reqParamsFormat(request);
            ReqGoodsBrand goodsBrandReq = build(params);

            int result = goodsService.saveBrandInfo(goodsBrandReq);
            respData = super.respParamsFormat(respCode, respMsg, result);
        } catch (Exception e) {
            e.printStackTrace();
            respCode = ResultStatus.BAD_REQUEST.getCode();
            respMsg = ResultStatus.BAD_REQUEST.getMsg();
            respData = super.respParamsFormat(respCode, respMsg, null);
        }
        resultMap.put("respData", respData);
        return resultMap;
    }

    /**
     * 封装入参信息
     *
     * @param params
     * @return
     */
    private ReqGoodsBrand build(JSONObject params) {
        return  JSONObject.toJavaObject(params,ReqGoodsBrand.class);
        /*ReqGoodsBrand goodsBrandReq = new ReqGoodsBrand();

        Long brandId = params.getLong("brandId");
        String brandName = params.getString("brandName");
        Integer brandType = params.getInteger("brandType");
        Integer categoryId = params.getInteger("categoryId");
        String brandLogo = params.getString("brandLogo");
        String siteUrl = params.getString("siteUrl");
        Integer sortOrl = params.getInteger("sortOrl");
        String brandDesc = params.getString("brandDesc");
        String beginTime = params.getString("beginTime");
        String endTime = params.getString("endTime");

        goodsBrandReq.setBrandId(brandId);
        goodsBrandReq.setBrandName(brandName);
        goodsBrandReq.setBrandType(brandType);
        goodsBrandReq.setCategoryId(categoryId);
        goodsBrandReq.setBrandLogo(brandLogo);
        goodsBrandReq.setSiteUrl(siteUrl);
        goodsBrandReq.setSortOrl(sortOrl);
        goodsBrandReq.setBrandDesc(brandDesc);
        goodsBrandReq.setBeginTime(beginTime);
        goodsBrandReq.setEndTime(endTime);
        return goodsBrandReq;*/
    }
}
